package com.tuling.jvm;



import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by chenhs on 2018/8/3.
 *
     jdk1.7及之后版本重现，方法区和运行常量池溢出...

    【运行时常量池导致的内存溢出异常】
     摘自:https://blog.csdn.net/sells2012/article/details/18656263

     方法区又交永久代，永久代溢出有以下几种情况：
     摘自:https://www.2cto.com/kf/201804/739761.html
     1.动态生成大量的class.
     2.大量jsp和动态产生jsp.

 * VM Args： -XX:PermSize=10M -XX:MaxPermSize=10M
 *
 */
public class JavaMethodAreaOOM {

    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                    return proxy.invokeSuper(obj, args);
                }
            });
            enhancer.create();
        }
    }

    static class OOMObject {

    }
}
